package echat.future.cn.echat.my.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import echat.future.cn.echat.R;
import echat.future.cn.echat.databinding.ActivityWithdrawHistoryBinding;
import echat.future.cn.echat.my.adapter.WithdrawAdapter;
import echat.future.cn.echat.my.api.UserApi;
import echat.future.cn.echat.my.bean.WithdrawBean;
import echat.future.cn.echat.my.utils.HttpUtilsRequst;
import echat.future.cn.echat.my.utils.UserHelper;
import echat.future.cn.echat.my.weight.Navagation.AdapterNavagation;
import echat.future.cn.echat.my.weight.Navagation.IBaseNavagation;

/**
 * 我的提现明细
 * @author 86936
 */
public class WithdrawHistoryActivity extends BaseActivity<ActivityWithdrawHistoryBinding>{

    private WithdrawAdapter mAdapter;

    private List<WithdrawBean> mDatas = new ArrayList<>();


    @Override
    public int layoutId() {
        return R.layout.activity_withdraw_history;
    }
    AdapterNavagation adapterNavagation;
    @Override
    public IBaseNavagation navagation() {
        adapterNavagation = new AdapterNavagation(this);
        adapterNavagation.setTitle("提现明细",16, R.color.white)
                .setNavagationBackgroudColor(R.color.color_161823).setBack(R.drawable.btn_back);
        return adapterNavagation;
    }

    @Override
    public void initView() {
        bind.swipeLayout.setOnRefreshListener(() -> {
            page = 1;
        });
        bind.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new WithdrawAdapter(mDatas);
        mAdapter.bindToRecyclerView(bind.recyclerView);
        loadData(1);
        mAdapter.setOnLoadMoreListener(() -> {
            page ++;
            loadData(page);
        },bind.recyclerView);
    }

    private void loadData(int page){
        if(page == 1){
            this.mDatas.clear();
            this.mAdapter.notifyDataSetChanged();
        }

        new HttpUtilsRequst<>(WithdrawHistoryActivity.this,HttpUtilsRequst.retrofitClient.create(UserApi.class).getWithrawList(UserHelper.getToken(),page+""))
                .setmOnRequstSuccessListener((HttpUtilsRequst.OnRequstSuccessListener<List<WithdrawBean>>) withdrawBeans -> {
                    mAdapter.loadMoreComplete();
                    change(withdrawBeans);
                    mAdapter.notifyDataSetChanged();
                    if(withdrawBeans.size() <10){
                        mAdapter.loadMoreEnd();
                    }
                }).start();
    }


    /**
     * 转换
     * @param withdrawBeans
     */
    private void change(List<WithdrawBean> withdrawBeans){
        for (WithdrawBean bean:withdrawBeans){
            addData(bean);
        }
    }


    /**
     * 添加数据
     * @param withdrawBean
     */
    private void addData(WithdrawBean withdrawBean){
        withdrawBean.setType(WithdrawBean.TYPE_DATA);
        //1添加月份视图
        if(mDatas.size() == 0
                || mDatas.get(mDatas.size()-1).getItemType() != WithdrawBean.TYPE_DATA
               ){
            if(mDatas.size() != 0 ){
                WithdrawBean monthBean = new WithdrawBean(null,WithdrawBean.TYPE_NONE);
                mDatas.add(monthBean);
            }
            Calendar calendar =  Calendar.getInstance();
            calendar.setTime(new Date(withdrawBean.getCreate_time()*1000));
           int month = calendar.get(Calendar.MONTH);//获取到月份
           WithdrawBean monthBean = new WithdrawBean((month+1)+"月",WithdrawBean.TYPE_MONTH);
            mDatas.add(monthBean);
            //添加标题
            WithdrawBean titleBen = new WithdrawBean(null,WithdrawBean.TYPE_TITLE);
            mDatas.add(titleBen);
            mDatas.add(withdrawBean);
        }else{
            //月份相同时候开始增加数据
            Calendar calendar =  Calendar.getInstance();
            calendar.setTime(new Date(withdrawBean.getCreate_time()*1000));
            int currentMonth = calendar.get(Calendar.MONTH);//获取到月份

            calendar.setTime(new Date(mDatas.get(mDatas.size()-1).getCreate_time()*1000));
            int latMonth = calendar.get(Calendar.MONTH);//获取到月份

            if(currentMonth != latMonth){
                WithdrawBean monthBean = new WithdrawBean((currentMonth+1)+"月",WithdrawBean.TYPE_MONTH);
                mDatas.add(monthBean);
                //添加标题
                WithdrawBean titleBen = new WithdrawBean(null,WithdrawBean.TYPE_TITLE);
                mDatas.add(titleBen);
                mDatas.add(withdrawBean);
            }else{
                //增加数据
                mDatas.add(withdrawBean);
                mDatas.add(withdrawBean);
            }
        }




    }

    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
}
